package com.gy.algorithm.carl.hashtableexa;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl
 * @Author: gy
 * @Date: 2024/10/22 11:30
 * @Description:
 */
public class TwoSum1 {

    public static void main(String[] args) {
        
    }

    // end1
    public static int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        Map<Integer,Integer> indexMapping = new HashMap<>();

        int need = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            need = target - nums[i];
            if(indexMapping.containsKey(need)){
                res[0] = i;
                res[1] = indexMapping.get(need);
                break;
            }else{
                indexMapping.put(nums[i],i);
            }
        }

        return res;
    }

    // write
    public static int[] twoSum1(int[] nums, int target) {
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target){
                   res[0] = i;
                   res[1] = j;
                   break;
                }
            }
        }
        return res;
    }
}
